package com.pounds.service.impl;

import com.pounds.mapper.UserInfoMapper;
import com.pounds.mapper.UserMapper;
import com.pounds.pojo.User;
import com.pounds.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    enum UserEnumError {
        Normal,
        DataFormatError,
        EmptyError,
        RepeatError,
        DatabaseError,
        DataNotFoundError,
        DataError
    }

    @Override
    public List<User> loopAllUser() {
        return userMapper.getUsers();
    }

    @Override
    public String register(String username, String account, String password) {
        if (username == null || account == null || password == null) {
            return UserEnumError.DataFormatError.toString();
        } else if (username.isEmpty() || account.isEmpty() || password.isEmpty()) {
            return UserEnumError.EmptyError.toString();
        }
        String otherAccount = userMapper.findUserAccount(account);
        if (otherAccount == null) {
            userMapper.newUser(username, account, password);
            userInfoMapper.newUserInfo(username, account);
            return UserEnumError.Normal.toString();
        } else if (otherAccount.isEmpty()) {
            return UserEnumError.DatabaseError.toString();
        } else if (account.equals(otherAccount)) {
            return UserEnumError.RepeatError.toString();
        } else {
            userMapper.newUser(username, account, password);
            userInfoMapper.newUserInfo(username, account);
            return UserEnumError.Normal.toString();
        }
    }

    @Override
    public String login(String account, String password) {
        if (account == null || password == null) {
            return UserEnumError.DataFormatError.toString();
        } else if (account.isEmpty() || password.isEmpty()) {
            return UserEnumError.EmptyError.toString();
        }
        String userPassword = userMapper.findUserPassword(account,password);
        if (userPassword == null){
            return UserEnumError.DataNotFoundError.toString();
        } else if (userPassword.isEmpty()) {
            return UserEnumError.DataNotFoundError.toString();
        } else if (userPassword.equals(password)) {
            return UserEnumError.Normal.toString();
        } else {
            return UserEnumError.DataError.toString();
        }
    }

    @Override
    public String ChangePassword(String account, String old_password, String new_password) {
        if (account == null || old_password == null || new_password == null){
            return UserEnumError.DataFormatError.toString();
        } else if (account.isEmpty() || old_password.isEmpty() || new_password.isEmpty()) {
            return UserEnumError.EmptyError.toString();
        } else if (old_password.equals(new_password)){
            return UserEnumError.RepeatError.toString();
        }
        String userAccount = userMapper.findUserAccount(account);
        if (userAccount == null){
            return UserEnumError.DataNotFoundError.toString();
        } else if (userAccount.isEmpty()) {
            return UserEnumError.DatabaseError.toString();
        } else if (account.equals(userAccount)){
            String userPassword = userMapper.findUserPassword(userAccount,new_password);
            if (userPassword == null){
                return UserEnumError.DataNotFoundError.toString();
            }else if(userPassword.isEmpty()){
                return UserEnumError.DatabaseError.toString();
            }else{
                userMapper.setUserPassword(account,new_password);
                return UserEnumError.Normal.toString();
            }
        }else{
            return UserEnumError.DatabaseError.toString();
        }
    }
}
